package com.codejam2008.qualification;

import java.io.File;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class SavingTheUniverse {

	static int nTest, S, Q;
	static String[] s;
	static int[] label;
	static int round, nSwitches;
	static Map<String, Integer> s2i;

	public static void main(String[] args) throws Exception {
		
		Scanner scanner = new Scanner(new File("input.txt"));
		PrintStream ps = new PrintStream(new File("output.txt"));
		System.setOut(ps);
		
		nTest = scanner.nextInt();
		
		for (int t=1;t<=nTest;++t) {
			S = scanner.nextInt();
			s = new String[S];
			label = new int[S];
			s2i = new HashMap<String, Integer>();
			scanner.nextLine();
			for (int i=0;i<S;++i) {
				s[i] = scanner.nextLine();
				s2i.put(s[i], i);
			}
			int count = S;
			round = 1;
			nSwitches = 0;
			Q = scanner.nextInt();
			scanner.nextLine();
			for (int i=0;i<Q;++i) {
				String se = scanner.nextLine();
				int idx = s2i.get(se);
				if (label[idx] != round) {
					label[idx] = round;
					count--;
					if (count == 0) {
						nSwitches++;
						label[idx]++;
						round++;
						count = S-1;
					}
				}
			}
			System.out.println("Case #" + t + ": " + nSwitches);
		}

	}

}
